<?php

namespace app\common\model;

use think\Model;

class WxappPop extends Model {

    public function getList(){
        return $this->with(['image'])->order(['image_sort' => 'desc'])->select();
    }

    public function getDefault(){
        return $this->with(['image'])->where(['is_default' => 1])->find();
    }

    public function image()
    {
        return $this->hasOne('uploadFile', 'file_id', 'image_id');
    }
    /**
     * 新增记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        if (empty($data['image_id'])) {
            $this->error = '请上传封面图';
            return false;
        }

        return $this->allowField(true)->save($data);
    }

    /**
     * 更新记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        if (empty($data['image_id'])) {
            $this->error = '请上传封面图';
            return false;
        }

        return $this->allowField(true)->save($data) !== false;
    }
    /**
     * 轮播图详情
     * @param $id
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail($id)
    {
        return self::get($id, ['image']);
    }

}